<template>
	<view>
		<view class="search-box">
			<my-search @click="gotoSearch"></my-search>
		</view>
		<!-- 轮播图 -->
		<swiper indicator-dots circular :interval="3000" :duration="1000">
			<swiper-item v-for="item in swiperList" :key="item.goods_id">
				<navigator class="swiper-item" :url="`/subpkg/goods_detail/goods_detail?goods_id=${item.goods_id}`">
					<image :src="item.image_src"></image>
				</navigator>
			</swiper-item>
		</swiper>
		<!-- 分类导航区域 -->
		<view class="navtive-item">
			<view v-for="(item,index) in navlist" :key="index" @click="navClickHandler(item)">
				<image :src="item.image_src"></image>
			</view>
		</view>
		<!-- 楼层数据 -->
		<view class="floor-list">
			<view class="floor-item" v-for="(item,index) in floorList" :key="index">
				<image class="floor-title" :src="item.floor_title.image_src"></image>
				<view class="floor-img-box">
					<!-- 左侧大图 -->
					<navigator class="left-img-box" :url="item.product_list[0].navigator_url">
						<image :src="item.product_list[0].image_src" :style="{width:item.product_list[0].image_width + 'rpx'}"
							mode="widthFix">
						</image>
					</navigator>
					<!-- 右侧小图 -->
					<view class="right-img-box">
						<navigator class="right-img-item" v-for="(item2, index2) in item.product_list" :key="index2"
							:url="item2.navigator_url">
							<image :src="item2.image_src" :style="{width:item2.image_width  + 'rpx'}" v-if="index2 !== 0"
								mode="widthFix"></image>
						</navigator>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import badgeMix from '@/mixins/tabbar-badge.js'
	export default {
		mixins: [badgeMix],
		data() {
			return {
				// 轮播图数据
				swiperList: [],
				// 分类导航的
				navlist: [],
				// 楼层数据
				floorList: []
			}
		},
		onLoad() {
			this.getSwiperList()
			this.getNavlist()
			this.getFloorList()
		},
		methods: {
			// 获取轮播图的数据
			async getSwiperList() {
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/home/swiperdata')
				// if (res.meta.status !== 200) {
				// 	// 错误信息,提示框
				// 	uni.showToast({
				// 		title: '数据请求失败!',
				// 		icon: 'none',
				// 		duration: 2000
				// 	})
				// }
				if (res.meta.status !== 200) return uni.$showMsg()

				this.swiperList = res.message || []

				// 通过.then 也行,修饰
				// const request = uni.$http.get('/api/public/v1/home/swiperdata')
				// request.then((res) => {
				// 	console.log(res, '加载数据');
				// })
			},
			/* 
			 获取分类导航的数据
			 */
			async getNavlist() {
				// 请求数据
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/home/catitems')
				if (res.meta.status !== 200) return uni.$showMsg()
				this.navlist = res.message || []
			},
			/* 
			 点击分类导航跳转到分类页面 
			 */
			navClickHandler(item) {
				if (item.name === '分类') {
					uni.switchTab({
						url: '/pages/cate/cate'
					})
				}
			},
			/* 
			 获取楼层数据
			 */
			async getFloorList() {
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/home/floordata')
				if (res.meta.status !== 200) return uni.$showMsg()
				this.floorList = res.message || []
				// 处理楼层跳转所需要的路径
				this.floorList.forEach(item => {
					item.product_list.map(v => {
						const splitArr = v.navigator_url.split('?')
						v.navigator_url = '/subpkg/goods_list/goods_list?' + splitArr[1]
						return v
					})
				})
			},
			/* 
			 跳转搜索页
			 */
			gotoSearch() {
				uni.navigateTo({
					url: '/subpkg/search/search'
				})
			},
		}
	}
</script>

<style lang="scss">
	.search-box {
		position: sticky;
		top: 0;
		z-index: 999;
	}

	swiper {
		height: 330rpx;

		.swiper-item,
		image {
			height: 100%;
			width: 100%;
			display: block;
		}
	}

	/* 分类导航 scss */
	.navtive-item {
		display: flex;
		justify-content: space-around;
		margin: 30rpx 0;

		image {
			height: 140rpx;
			width: 128rpx;
			display: block;
		}
	}

	/* 楼层数据 scss */
	.floor-list {
		.floor-item {
			.floor-title {
				width: 100%;
				height: 60rpx;
				display: flex;
			}

			.floor-img-box {
				display: flex;
				padding-left: 10rpx;

				image {
					display: block;
				}

				.right-img-box {
					display: flex;
					flex-wrap: wrap;
					justify-content: space-around;
				}
			}
		}
	}
</style>